package com.esms.controller;

import com.esms.po.Department;
import com.esms.po.Employee;
import com.esms.po.Responsibility;
import com.esms.service.DepartmentService;
import com.esms.service.EmployeeService;
import com.esms.service.IResponsibilityService;
import com.esms.utils.Message;
import com.esms.utils.Text2Html;
import com.esms.vo.ResponsibilityChangeVo;
import com.esms.vo.ResponsibilityDepVo;
import com.esms.vo.ResponsibilityJobVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

@Controller
@RequestMapping("/responsibility")
    public class ResponsibilityController {

    @Autowired
    EmployeeService employeeService;

    @Autowired
    DepartmentService departmentService;


    @Autowired
    IResponsibilityService responsibilityService;

    /**
     * 获取用户的部门职责
     *
     * @param httpSession
     * @return
     */
    @GetMapping("getDepartmentResp")
    @ResponseBody
    public Object GetUsersDepartmentResp(HttpSession httpSession) {
        try {
            Integer id = (Integer) httpSession.getAttribute("employeeId");
            Employee employee = employeeService.getById(id);
            Department department = departmentService.selectByPrimaryKey(employee.getdId());
            String resp = responsibilityService.getRespByUserId(id);
            resp = Text2Html.TextToHtml(resp);
            HashMap<String, String> map = new HashMap<>();
            map.put("depResponsibility", resp);
            map.put("depName",department.getdName());
            return map;
        } catch (Exception e) {
            return Message.error("getDepartmentResp Wrong!");
        }
    }

    /**
     * 添加部门责任
     *
     * @param responsibilityDepVo
     * @return
     */
    @PostMapping("addDepResp")
    @ResponseBody
    public Message AddDepResp(@RequestBody ResponsibilityDepVo responsibilityDepVo) {
        try {
            if (responsibilityService.checkIfExist(responsibilityDepVo.getDepId(),0)){
                return Message.error(new String( "该部门责任已经存在".getBytes(StandardCharsets.UTF_8) , StandardCharsets.UTF_8));
            }else {
                responsibilityService.AddDepResp(responsibilityDepVo);
            }
            return Message.success("添加成功");
        } catch (Exception e) {
            return Message.error("addDepResp Wrong!");
        }
    }

    /**
     * 添加岗位责任
     *
     * @param responsibilityJobVo
     * @return
     */
    @PostMapping("addJobResp")
    @ResponseBody
    public Message AddJobResp(@RequestBody ResponsibilityJobVo responsibilityJobVo) {
        try {
            if (responsibilityService.checkIfExist(responsibilityJobVo.getDepId(), responsibilityJobVo.getJobId())){
                return Message.error("该部门下的岗位责任已经存在");
            }else {
                responsibilityService.AddJobResp(responsibilityJobVo);
            }
            return Message.success("添加成功");
        } catch (Exception e) {
            return Message.error("addJobResp Wrong!");
        }
    }

    /**
     * 查询数量
     *
     * @param type
     * @return
     */
    @GetMapping("countResp/{type}")
    @ResponseBody
    public Object countRespByType(@PathVariable(value = "type") String type) {
        HashMap<String, Integer> map = new HashMap<>();
        switch (type) {
            case "dep":
                map.put("count",responsibilityService.countRep());
                return map;
            case "job":
                map.put("count",responsibilityService.countJob());
                return map;
        }
        return Message.fail();
    }

    /**
     * 分页查询岗位责任
     *
     * @param pageSize 分页大小
     * @param count    页码
     * @param depId    部门id
     * @param jobId    岗位id
     * @return
     */
    @GetMapping("pageQueryJobResp")
    @ResponseBody
    public Object pageQueryJobResp(@RequestParam(value = "pageSize", required = true) Integer pageSize,
                                    @RequestParam(value = "pageCount", required = true) Integer count,
                                    @RequestParam(value = "depId", required = false) Integer depId,
                                    @RequestParam(value = "jobId", required = false) Integer jobId
    ) {

        try {
            return responsibilityService.PageJobResp(pageSize, count, depId, jobId);
        }catch (Exception e){
            return Message.fail();
        }

    }

    /**
     * 分页查询部门责任
     *
     * @param pageSize
     * @param count
     * @param depId
     * @return
     */
    @GetMapping("pageQueryDepResp")
    @ResponseBody
    public Object pageQueryDepResp(@RequestParam(value = "pageSize", required = true) Integer pageSize,
                                    @RequestParam(value = "pageCount", required = true) Integer count,
                                    @RequestParam(value = "depId", required = false) Integer depId
    ) {
        try {
            return responsibilityService.PageDepResp(pageSize, count, depId);
        }catch (Exception e){
            return Message.fail();
        }
    }


    /**
     * 修改责任
     * @param responsibilityChangeVo
     * @return
     */
    @PostMapping("changeResp")
    @ResponseBody
    public Message changeResp(@RequestBody ResponsibilityChangeVo responsibilityChangeVo) {
        try {
            if (responsibilityService.checkIfExistExcluedId(responsibilityChangeVo.getDepId(), responsibilityChangeVo.getJobId(),responsibilityChangeVo.getId())){
                return Message.error("该责任已经存在");
            }else {
                responsibilityService.updateResp(responsibilityChangeVo);
            }
            return Message.success();
        } catch (Exception e) {
            return Message.error("changeResp Wrong!");
        }
    }

    @GetMapping("DelResp")
    @ResponseBody
    public Message DelResp(@RequestParam(value = "id",required = true) Integer id) {
        try {
            responsibilityService.DelById(id);
            return Message.success();
        }catch (Exception e ){
            return Message.fail();
        }
    }
}
